package com.guchenbo.example;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;

import java.util.Map;

public class Test2 {

    public static void main(String[] args) {
        JSONObject js = new JSONObject();
        js.put("mappings", new JSONObject());
        JSONObject properties = new JSONObject();
        properties.fluentPut("class", new JSONObject().fluentPut("type", "keyword"));
        properties.fluentPut("predictor", new JSONObject().fluentPut("type", "keyword"));
        properties.fluentPut("probility_1", new JSONObject().fluentPut("type", "double"));
        for (int i = 0; i < 11; i++) {
            properties.fluentPut("threshold_" + i, new JSONObject().fluentPut("type", "keyword"));
        }

        js.getJSONObject("mappings").fluentPut("properties", properties);
        System.out.println(js);

        JSONObject json = new JSONObject();
        json.putIfAbsent("aggs", new JSONObject());
        json.getJSONObject("aggs").put("cm", buildTerms("predictor", true));

        for (int i = 0; i < 11; i++) {
            json.getJSONObject("aggs").put("cm_" + i, buildTerms("threshold_" + i, true));
        }
        System.out.println(json);

        System.out.println(buildTerms("th", false));

        System.out.println("#############");
        parse();
    }

    private static void parse() {
        String response = "{\"took\":20,\"timed_out\":false,\"_shards\":{\"total\":1,\"successful\":1,\"skipped\":0,\"failed\":0},\"hits\":{\"total\":{\"value\":251,\"relation\":\"eq\"},\"max_score\":null,\"hits\":[]},\"aggregations\":{\"cm_3\":{\"doc_count_error_upper_bound\":0,\"sum_other_doc_count\":0,\"buckets\":[{\"key\":\"1\",\"doc_count\":131,\"actual\":{\"doc_count_error_upper_bound\":0,\"sum_other_doc_count\":0,\"buckets\":[{\"key\":\"1\",\"doc_count\":114},{\"key\":\"0\",\"doc_count\":17}]}},{\"key\":\"0\",\"doc_count\":120,\"actual\":{\"doc_count_error_upper_bound\":0,\"sum_other_doc_count\":0,\"buckets\":[{\"key\":\"0\",\"doc_count\":112},{\"key\":\"1\",\"doc_count\":8}]}}]},\"cm_4\":{\"doc_count_error_upper_bound\":0,\"sum_other_doc_count\":0,\"buckets\":[{\"key\":\"0\",\"doc_count\":127,\"actual\":{\"doc_count_error_upper_bound\":0,\"sum_other_doc_count\":0,\"buckets\":[{\"key\":\"0\",\"doc_count\":118},{\"key\":\"1\",\"doc_count\":9}]}},{\"key\":\"1\",\"doc_count\":124,\"actual\":{\"doc_count_error_upper_bound\":0,\"sum_other_doc_count\":0,\"buckets\":[{\"key\":\"1\",\"doc_count\":113},{\"key\":\"0\",\"doc_count\":11}]}}]},\"cm_10\":{\"doc_count_error_upper_bound\":0,\"sum_other_doc_count\":0,\"buckets\":[{\"key\":\"0\",\"doc_count\":152,\"actual\":{\"doc_count_error_upper_bound\":0,\"sum_other_doc_count\":0,\"buckets\":[{\"key\":\"0\",\"doc_count\":129},{\"key\":\"1\",\"doc_count\":23}]}},{\"key\":\"1\",\"doc_count\":99,\"actual\":{\"doc_count_error_upper_bound\":0,\"sum_other_doc_count\":0,\"buckets\":[{\"key\":\"1\",\"doc_count\":99}]}}]},\"cm_1\":{\"doc_count_error_upper_bound\":0,\"sum_other_doc_count\":0,\"buckets\":[{\"key\":\"1\",\"doc_count\":143,\"actual\":{\"doc_count_error_upper_bound\":0,\"sum_other_doc_count\":0,\"buckets\":[{\"key\":\"1\",\"doc_count\":116},{\"key\":\"0\",\"doc_count\":27}]}},{\"key\":\"0\",\"doc_count\":108,\"actual\":{\"doc_count_error_upper_bound\":0,\"sum_other_doc_count\":0,\"buckets\":[{\"key\":\"0\",\"doc_count\":102},{\"key\":\"1\",\"doc_count\":6}]}}]},\"cm_2\":{\"doc_count_error_upper_bound\":0,\"sum_other_doc_count\":0,\"buckets\":[{\"key\":\"1\",\"doc_count\":137,\"actual\":{\"doc_count_error_upper_bound\":0,\"sum_other_doc_count\":0,\"buckets\":[{\"key\":\"1\",\"doc_count\":116},{\"key\":\"0\",\"doc_count\":21}]}},{\"key\":\"0\",\"doc_count\":114,\"actual\":{\"doc_count_error_upper_bound\":0,\"sum_other_doc_count\":0,\"buckets\":[{\"key\":\"0\",\"doc_count\":108},{\"key\":\"1\",\"doc_count\":6}]}}]},\"cm_0\":{\"doc_count_error_upper_bound\":0,\"sum_other_doc_count\":0,\"buckets\":[{\"key\":\"1\",\"doc_count\":251,\"actual\":{\"doc_count_error_upper_bound\":0,\"sum_other_doc_count\":0,\"buckets\":[{\"key\":\"0\",\"doc_count\":129},{\"key\":\"1\",\"doc_count\":122}]}}]},\"cm_9\":{\"doc_count_error_upper_bound\":0,\"sum_other_doc_count\":0,\"buckets\":[{\"key\":\"0\",\"doc_count\":139,\"actual\":{\"doc_count_error_upper_bound\":0,\"sum_other_doc_count\":0,\"buckets\":[{\"key\":\"0\",\"doc_count\":127},{\"key\":\"1\",\"doc_count\":12}]}},{\"key\":\"1\",\"doc_count\":112,\"actual\":{\"doc_count_error_upper_bound\":0,\"sum_other_doc_count\":0,\"buckets\":[{\"key\":\"1\",\"doc_count\":110},{\"key\":\"0\",\"doc_count\":2}]}}]},\"cm\":{\"doc_count_error_upper_bound\":0,\"sum_other_doc_count\":0,\"buckets\":[{\"key\":\"0\",\"doc_count\":132,\"actual\":{\"doc_count_error_upper_bound\":0,\"sum_other_doc_count\":0,\"buckets\":[{\"key\":\"0\",\"doc_count\":122},{\"key\":\"1\",\"doc_count\":10}]}},{\"key\":\"1\",\"doc_count\":119,\"actual\":{\"doc_count_error_upper_bound\":0,\"sum_other_doc_count\":0,\"buckets\":[{\"key\":\"1\",\"doc_count\":112},{\"key\":\"0\",\"doc_count\":7}]}}]},\"cm_7\":{\"doc_count_error_upper_bound\":0,\"sum_other_doc_count\":0,\"buckets\":[{\"key\":\"0\",\"doc_count\":136,\"actual\":{\"doc_count_error_upper_bound\":0,\"sum_other_doc_count\":0,\"buckets\":[{\"key\":\"0\",\"doc_count\":125},{\"key\":\"1\",\"doc_count\":11}]}},{\"key\":\"1\",\"doc_count\":115,\"actual\":{\"doc_count_error_upper_bound\":0,\"sum_other_doc_count\":0,\"buckets\":[{\"key\":\"1\",\"doc_count\":111},{\"key\":\"0\",\"doc_count\":4}]}}]},\"cm_8\":{\"doc_count_error_upper_bound\":0,\"sum_other_doc_count\":0,\"buckets\":[{\"key\":\"0\",\"doc_count\":137,\"actual\":{\"doc_count_error_upper_bound\":0,\"sum_other_doc_count\":0,\"buckets\":[{\"key\":\"0\",\"doc_count\":126},{\"key\":\"1\",\"doc_count\":11}]}},{\"key\":\"1\",\"doc_count\":114,\"actual\":{\"doc_count_error_upper_bound\":0,\"sum_other_doc_count\":0,\"buckets\":[{\"key\":\"1\",\"doc_count\":111},{\"key\":\"0\",\"doc_count\":3}]}}]},\"cm_5\":{\"doc_count_error_upper_bound\":0,\"sum_other_doc_count\":0,\"buckets\":[{\"key\":\"0\",\"doc_count\":132,\"actual\":{\"doc_count_error_upper_bound\":0,\"sum_other_doc_count\":0,\"buckets\":[{\"key\":\"0\",\"doc_count\":122},{\"key\":\"1\",\"doc_count\":10}]}},{\"key\":\"1\",\"doc_count\":119,\"actual\":{\"doc_count_error_upper_bound\":0,\"sum_other_doc_count\":0,\"buckets\":[{\"key\":\"1\",\"doc_count\":112},{\"key\":\"0\",\"doc_count\":7}]}}]},\"cm_6\":{\"doc_count_error_upper_bound\":0,\"sum_other_doc_count\":0,\"buckets\":[{\"key\":\"0\",\"doc_count\":134,\"actual\":{\"doc_count_error_upper_bound\":0,\"sum_other_doc_count\":0,\"buckets\":[{\"key\":\"0\",\"doc_count\":123},{\"key\":\"1\",\"doc_count\":11}]}},{\"key\":\"1\",\"doc_count\":117,\"actual\":{\"doc_count_error_upper_bound\":0,\"sum_other_doc_count\":0,\"buckets\":[{\"key\":\"1\",\"doc_count\":111},{\"key\":\"0\",\"doc_count\":6}]}}]}}}";
        JSONObject aggregation = JSON.parseObject(response).getJSONObject("aggregations");

        Map<String, JSONObject> map = Maps.newHashMap();
        for (String k : aggregation.keySet()) {
            map.put(k, wrapRoc(aggregation.getJSONObject(k)));
        }
        System.out.println(map);
    }

    private static JSONObject wrapRoc(JSONObject jsonObject) {
        JSONObject ret = new JSONObject();
        JSONArray buckets = jsonObject.getJSONArray("buckets");
        buckets.stream().forEach(j -> {
            JSONObject js = (JSONObject) j;
            JSONArray b = js.getJSONObject("actual").getJSONArray("buckets");
            String valP = js.getString("key");
            b.stream().forEach(j2 -> {
                JSONObject js2 = (JSONObject) j2;
                String valT = js2.getString("key");
                int cnt = js2.getIntValue("doc_count");
                if (valP.equals("1")) {
                    if (valT.equals("1")) {
                        ret.put("tp", cnt);
                    } else {
                        ret.put("fp", cnt);
                    }
                } else {
                    if (valT.equals("1")) {
                        ret.put("fn", cnt);
                    } else {
                        ret.put("tn", cnt);
                    }
                }
            });
        });
        return ret;
    }

    public static JSONObject buildTerms(String field, boolean re) {
        JSONObject json = new JSONObject();
        json.putIfAbsent("terms", new JSONObject().fluentPut("field", field));
        if (re) {
            json.putIfAbsent("aggs", new JSONObject());
            json.getJSONObject("aggs").fluentPut("actual", buildTerms("class", false));
        }
        return json;
    }
}
